package com.soul.dj.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import com.soul.dj.MainApplication;
import com.soul.dj.R;
import com.soul.dj.model.User;
import com.soul.dj.util.Const;
import com.soul.dj.util.http.JSONCallBack;
import com.soul.dj.util.http.MediaType;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by SOUL on 2017/2/13.
 */

public class LoginActivity extends BaseActivity {

    private EditText nameEdit,passwordEdit;

    private ImageView clearBtn,eyeBtn;

    @Override
    protected void initView() {
        setContentView(R.layout.login_activity);

        nameEdit = (EditText) findViewById(R.id.name);
        passwordEdit = (EditText) findViewById(R.id.password);

        clearBtn = (ImageView) findViewById(R.id.clearBtn);
        eyeBtn = (ImageView) findViewById(R.id.eyeBtn);

        clearBtn.setOnClickListener(this);
        eyeBtn.setOnClickListener(this);

        findViewById(R.id.login).setOnClickListener(this);

        nameEdit.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // 输入前的监听

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听
                if (!TextUtils.isEmpty(s)) {
                    if (clearBtn != null) {
                        clearBtn.setVisibility(View.VISIBLE);

                    }
                } else {
                    if (clearBtn != null) {
                        clearBtn.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.clearBtn:
                nameEdit.getText().clear();
                break;
            case R.id.eyeBtn:
                int isVisible = passwordEdit.getInputType();
                int cursorPosition = passwordEdit.length();
                if (isVisible == (InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD)) {
                    eyeBtn.setBackgroundResource(R.drawable.eye);
                    passwordEdit.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    passwordEdit.setSelection(cursorPosition);
                }else{
                    eyeBtn.setBackgroundResource(R.drawable.eye_close);
                    passwordEdit.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    passwordEdit.setSelection(cursorPosition);
                }
                break;
            case R.id.login:
                String name = nameEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                if (TextUtils.isEmpty(name)){
                    toast(nameEdit.getHint().toString());
                    return;
                }
                if (TextUtils.isEmpty(password)){
                    toast(passwordEdit.getHint().toString());
                    return;
                }




                Map<String,Object> paramsMap = new HashMap<>();
                paramsMap.put("logid",name);
                paramsMap.put("passwd",password);
//                sendHttp(Const.REQUEST_URL_LOGIN,MediaType.JSON,paramsMap,new JSONCallBack<User>(this,"login", User.class));

                break;
        }
    }


    @Override
    public void onAjaxOK(Object result, String tag) {
        super.onAjaxOK(result, tag);

        if (TextUtils.equals(tag,"login")){
            User user = (User) result;
            MainApplication.addMap(User.LOGID,user.getLogid());
            MainApplication.addMap(User.LOGTIME,user.getLogtime());

            startActivity(new Intent(this,MainActivity.class));
            MainApplication.removeActivity(this);
        }
    }
}
